package com.huiqiao.sys.controller;

import com.huiqiao.sys.constant.SysConstant;
import com.huiqiao.sys.domain.Menu;
import com.huiqiao.sys.domain.MenuVo;
import com.huiqiao.sys.domain.TreeNode;
import com.huiqiao.sys.domain.User;
import com.huiqiao.sys.service.MenuService;
import com.huiqiao.sys.utils.DataGridView;
import com.huiqiao.sys.utils.ResultObj;
import com.huiqiao.sys.utils.TreeNodeBuilder;
import com.huiqiao.sys.utils.WebUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-22  09:56
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    @RequestMapping("loadIndexLeftMenuJson")
    public List<TreeNode> loadIndexLeftMenuJson(MenuVo menuVo) {
        //得到当前用户登录的对象
        User user = (User) WebUtils.getHttpSession().getAttribute("user");
        //查询所有可用菜单
        List<Menu> menus = null;
        menuVo.setAvailable(SysConstant.AVAILABLE_TRUE);
        if (user.getType() == SysConstant.USER_TYPE_SUPER){
            menus = menuService.queryAllMenuForList(menuVo);
        }else {

            //TODO 用户不是超级管理员，展示的菜单页面不同，添加权限时候进行补充
            menus = menuService.queryMenuByUserIdForList(user.getUserid());

        }
        //把list里面的数据放进nodes
        List<TreeNode> nodes = new ArrayList<>();
        for (Menu menu : menus) {
            Integer id = menu.getId();
            Integer pid = menu.getPid();
            String title = menu.getTitle();
            String icon = menu.getIcon();
            String href = menu.getHref();
            Boolean spread = menu.getSpread() == SysConstant.SPREAD_FALSE ? false : true;
            String target = menu.getTarget();
            nodes.add(new TreeNode(id, pid, title, icon, href, spread, target));
        }

        List<TreeNode> treeNodes = menus.stream().map(item -> {
            TreeNode treeNode = new TreeNode();
            BeanUtils.copyProperties(item, treeNode);
            treeNode.setSpread(item.getSpread() == SysConstant.SPREAD_FALSE ? false : true);
            return treeNode;
        }).collect(Collectors.toList());

        return TreeNodeBuilder.builder(nodes, 1);
    }


    @RequestMapping("loadMenuManagerLeftTreeJson")
    public DataGridView loadMenuManagerLeftTreeJson() {
        List<Menu> menu = menuService.queryAll();

        List<TreeNode> treeNodes = menu.stream().map(item -> {
            TreeNode treeNode = new TreeNode();
            BeanUtils.copyProperties(item, treeNode);
            treeNode.setSpread(item.getSpread() == SysConstant.SPREAD_FALSE ? false : true);
            return treeNode;
        }).collect(Collectors.toList());

        return new DataGridView(treeNodes);
    }


    /**
     * 查询是否有子菜单，有子菜单不能直接删除
     * @author: yanliwei@foolish
     * @date: 2024/12/25 星期三 11:57
     */
    @RequestMapping("checkMenuHasChildren")
    public ResultObj checkMenuHasChildren(Integer id) {
        // 根据id查询菜单
        Menu menu = menuService.queryMenuById(id);
        if (menu == null) {
            // 如果菜单不存在，返回错误信息
            return new ResultObj(-1, "菜单不存在");
        }
        // 查询所有菜单
        List<Menu> menus = menuService.queryAll();
        // 检查是否有菜单的pid等于当前菜单的id
        boolean hasChildren = menus.stream().anyMatch(m -> m.getPid() != null && m.getPid().equals(id));
        if (hasChildren) {
            // 如果有子菜单，返回状态码表示有子菜单
            return ResultObj.STATUS_TRUE;
        } else {
            // 如果没有子菜单，返回状态码表示没有子菜单
            return ResultObj.STATUS_FALSE;
        }
    }


    /*@RequestMapping("checkMenuHasChildren")
    public ResultObj checkMenuHasChildren(Integer id) {
        //根据id查询菜单
        Menu menu = menuService.queryMenuById(id);
        //查询所有菜单
        List<Menu> menus = menuService.queryAll();
        Stream<Integer> integerStream = menus.stream().map(Menu::getId);
        if (integerStream.anyMatch(integer -> integer.equals(id))){
            return ResultObj.STATUS_TRUE;
        }else {
            return ResultObj.STATUS_FALSE;
        }

    }*/


    @RequestMapping("loadAllMenu")
    public DataGridView loadAllMenu(MenuVo menuVo) {
        return this.menuService.queryAllMenu(menuVo);
    }


    @RequestMapping("addMenu")
    public ResultObj addMenu(MenuVo menuVo) {
        try {
            this.menuService.addMenu(menuVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            return ResultObj.ADD_ERROR;
        }
    }

    //删除
    @RequestMapping("deleteMenu")
    public ResultObj deleteMenu(MenuVo menuVo) {
        try {
            this.menuService.deleteMenu(menuVo);
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            return ResultObj.DELETE_ERROR;
        }
    }


    @RequestMapping("updateMenu")
    public ResultObj updateMenu(MenuVo menuVo) {
        try {
            this.menuService.updateMenu(menuVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            return ResultObj.UPDATE_ERROR;
        }
    }




}
